/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.XPath;
using System.ComponentModel.Design;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.TypeConverters.BuiltIn
{
	internal sealed class IfThenComponentTypeConverter : TypeConverter
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		private SandcastleBuildComponentManager thenManager, elseManager;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="IfThenComponentTypeConverter" /> class.
		/// </summary>
		public IfThenComponentTypeConverter()
		{
		}
		#endregion

		#region Methods
		public override bool GetPropertiesSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
		{
			PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);
			SandcastleBuildComponent component = value as SandcastleBuildComponent;

			if (context != null && component != null)
			{
				EnsureManagers(component);

				properties.Add(new NestedComponentsPropertyDescriptor("Then", Resources.Text.IfThenComponentThenName, Resources.Text.IfThenComponentThenDescription, thenManager, context));
				properties.Add(new NestedComponentsPropertyDescriptor("Else", Resources.Text.IfThenComponentElseName, Resources.Text.IfThenComponentElseDescription, elseManager, context));
			}

			return properties;
		}

		private void EnsureManagers(SandcastleBuildComponent component)
		{
			SandcastleBuildComponentManager parent = component.Manager;

			XPathNavigator thenNavigator = component.Configuration.CreateNavigator();
			XPathNavigator elseNavigator = thenNavigator.CreateNavigator();

			if (thenNavigator.MoveToChild("then", ""))
			{
				if (thenManager == null)
					thenManager = new SandcastleBuildComponentManager(component.LineNumber, component, parent, thenNavigator.CreateNavigator());
			}
			else
			{
				if (thenNavigator.MoveToChild("if", ""))
				{
					thenNavigator.InsertElementAfter(null, "then", null, null);
					thenNavigator.MoveToNext(XPathNodeType.Element);
				}
				else
				{
					thenNavigator.PrependChildElement(null, "then", null, null);
					thenNavigator.MoveToChild(XPathNodeType.Element);
				}

				thenManager = new SandcastleBuildComponentManager(component.LineNumber, component, parent, thenNavigator.CreateNavigator());
			}

			if (elseNavigator.MoveToChild("else", ""))
			{
				if (elseManager == null)
					elseManager = new SandcastleBuildComponentManager(component.LineNumber, component, parent, elseNavigator.CreateNavigator());
			}
			else
			{
				elseNavigator.AppendChildElement(null, "else", null, null);
				elseNavigator.MoveToChild("else", "");

				elseManager = new SandcastleBuildComponentManager(component.LineNumber, component, parent, elseNavigator.CreateNavigator());
			}
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				IXPathNavigable navigable = value as IXPathNavigable;

				if (navigable != null)
				{
					XPathNavigator navigator = navigable.CreateNavigator();
					
					if (navigator.MoveToChild("if", ""))
						return navigator.GetAttribute("condition", "");
				}

				return string.Empty;
			}
			else
				return base.ConvertTo(context, culture, value, destinationType);
		}
		#endregion
	}
}
